package com.entrance.menuadapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.landmesure.bluetoothchat.R;
import com.widget.SelectCallBack;

import java.util.ArrayList;

public class MenuAdapter extends BaseExpandableListAdapter implements SelectCallBack{
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<GroupType> mGroupTypes;
    private int mPosition = MenuFragment.createItemId(0, 0);
    MenuAdapter(Context context, LayoutInflater inflater, ArrayList<GroupType> groupTypes) {
        mContext = context;
        mInflater = inflater;
        mGroupTypes = groupTypes;
    }

    @Override
    public int getGroupCount() {
        return mGroupTypes.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mGroupTypes.get(groupPosition).getItemSize();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroupTypes.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mGroupTypes.get(groupPosition).getItem((int) getChildId(groupPosition, childPosition));
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return MenuFragment.createItemId(groupPosition, childPosition);
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = initHolder(convertView, R.layout.layout_menu_type_group,
                parent, R.id.menu_type_name);
        viewHolder.textView.setText(((GroupType) getGroup(groupPosition)).getName());
        convertView = viewHolder.convertView;
        convertView.setClickable(true);
        convertView.setBackgroundColor(mContext.getResources().getColor(R.color.colorDivider));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = initHolder(convertView, R.layout.layout_menu_list_item,
               parent, R.id.menu_list_item_name);
        if ((int) getChildId(groupPosition, childPosition) == mPosition) {
            viewHolder.textView.setTextColor(Color.BLUE);
        } else {
            viewHolder.textView.setTextColor(Color.DKGRAY);
        }
        viewHolder.textView.setText(((Item) getChild(groupPosition, childPosition)).getName());
        convertView = viewHolder.convertView;
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private ViewHolder initHolder(View convertView, int lId, ViewGroup parent, int wId) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = mInflater.inflate(lId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.textView = convertView.findViewById(wId);
            viewHolder.convertView = convertView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        return viewHolder;
    }

    @Override
    public void setSelectItem(int itemID) {
        mPosition = itemID;
        notifyDataSetChanged();
    }

    private class ViewHolder {
        View convertView;
        TextView textView;
    }
}
